/**
 * Created by 66xiaoqu on 2015/12/31.
 */
var async = require('async');
var mysqlUtil = require('../../../common/utils/mysqlUtil');


/***
 *
 select service_code, service_name,url
 ,avg(time) avg_time,max(time) max_time
 ,avg(node_time) avg_node_time,max(node_time) max_node_time
 from 66xiaoqu_log.app_request_log A
 where 1=1 and service_code is not null
 -- and create_time >= UNIX_TIMESTAMP(curdate())*1000 --当天
 and (
 create_time >=UNIX_TIMESTAMP(adddate(current_date, interval -1 day))*1000
 and create_time< UNIX_TIMESTAMP(curdate())*1000
 )
 -- and create_time >=UNIX_TIMESTAMP(adddate(current_date, interval -7 day))*1000  -- 七天内
 -- and create_time >=UNIX_TIMESTAMP(adddate(current_date, interval -30 day))*1000    -- 30天内
 group by service_code;
 */
exports.apiTimeCategoryService = function (req, callback) {
    var body = req.body;
    var apiCategory = body.category;

    var size = Number(req.params.size);
    var index = (Number(req.params.page) - 1) * size;

    logger.debug(body);

    var timeEnd;
    switch (apiCategory)
    {
        case "rankTime":
        {
            timeEnd = body.timeEnd;
            if(!timeEnd)
            {
                timeEnd = new Date(new Date).getTime();
            }
        }
        default:
        {
            if (!apiCategory) {
                apiCategory = "today";
            }
        }
    }

    var sql_type = {
        today: " create_time >= UNIX_TIMESTAMP(curdate())*1000",
        tomorrow: " create_time >=UNIX_TIMESTAMP(adddate(current_date, interval -1 day))*1000 and create_time< UNIX_TIMESTAMP(curdate())*1000",
        three_before: " create_time >=UNIX_TIMESTAMP(adddate(current_date, interval -3 day))*1000",
        week: " create_time >=UNIX_TIMESTAMP(adddate(current_date, interval -7 day))*1000",
        rankTime:" create_time >= " + body.timeStart + "  and create_time < " + timeEnd
    }

    //console.log(apiCategory);
    var sql_current_list = "select service_code, service_name,url,avg(time) avg_time,max(time) max_time,avg(node_time) avg_node_time,max(node_time) max_node_time " +
        " from 66xiaoqu_log.app_request_log A where 1=1 and service_code is not null and " +
        sql_type[apiCategory] +
        " group by service_code";

    var sql_count = "select count(*) as count from (" +
        sql_current_list +
        ") a";

    sql_current_list = 'select * from (' + sql_current_list +')V order by avg_node_time desc,avg_time desc ';

    var sql_count_list_limit = sql_current_list + " limit " + index + "," + size;
    //console.log("sql_current_list ", sql_current_list);

    var sql={};
    sql.sqlcount = sql_count;
    sql.sqllistlimit  = sql_count_list_limit;

    apiServiceHelper(req,sql,callback);
}

//exports.apiTimeRankService = function (req, callback) {
    //var body = req.body;
    //var size = Number(req.params.size);
    //var index = (Number(req.params.page) - 1) * size;
    //
    //var timeStart = body.timeStart;
    //var timeEnd = body.timeEnd;
    //if(!timeEnd)
    //{
    //    timeEnd = new Date(new Date).getTime();
    //}
    //var sqlCondition = " create_time >= " + timeStart + "  and create_time < " + timeEnd;
    //var sqlListNow = " select service_code, service_name,url,avg(time) avg_time,max(time) max_time,avg(node_time) avg_node_time,max(node_time) max_node_time  from 66xiaoqu_log.app_request_log A where 1=1 and service_code is not null and  (" +
    //    sqlCondition +
    //    ") group by service_code";
    //
    //var sql_count = "select count(*) as count from (" +
    //    sqlListNow +
    //    ") a";
    //var sql_count_list_limit = sqlListNow + " limit " + index + "," + size;
    //logger.debug(sqlListNow);
    //
    //var sql={};
    //sql.sqlcount = sql_count;
    //sql.sqllistlimit  = sql_count_list_limit;
    //
    //apiServiceHelper(req,sql,callback);
//    this.apiTimeCategoryService(req, callback);
//
//};

exports.apiCodeCategoryService = function(req,callback) {
    var body = req.body;
    var apiCategory = body.category;
    var code = Number(body.code);

    console.log(apiCategory)
    var size = Number(req.params.size);
    var index = (Number(req.params.page) - 1) * size;


    var sqlCondition = " service_code = " + code;
    var sql_type;
    if(apiCategory !== "rankTime"){
        sql_type = {
        today: " create_time >= UNIX_TIMESTAMP(curdate())*1000",
        tomorrow: " create_time >=UNIX_TIMESTAMP(adddate(current_date, interval -1 day))*1000 and create_time< UNIX_TIMESTAMP(curdate())*1000",
        three_before: " create_time >=UNIX_TIMESTAMP(adddate(current_date, interval -3 day))*1000",
        week: " create_time >=UNIX_TIMESTAMP(adddate(current_date, interval -7 day))*1000"
        };
    }
    else {
        var timeStart = body.timeStart;
        var timeEnd = body.timeEnd;
        if(!timeEnd)
        {
            timeEnd = new Date(new Date).getTime();
        }

        sql_type={}
        sql_type.rankTime = " (create_time >= " + timeStart + "  and create_time < " + timeEnd +") ";
    }

    var sqlListNow = " select service_code, service_name,url,time ,node_time,net_type,net_provider,type,vname,FROM_UNIXTIME(create_time/1000,'%Y-%m-%d %H:%i:%s')create_time  from 66xiaoqu_log.app_request_log A where 1=1 and service_code is not null and  (" +
        sqlCondition + " and "+
        sql_type[apiCategory]+
        " ) ";

    var sql_count = "select count(*) as count from (" +
        sqlListNow +
        ") a";

    sqlListNow = 'select * from (' + sqlListNow +')V order by node_time desc,time desc ';

    var sql_count_list_limit = sqlListNow + " limit " + index + "," + size;
    logger.debug(sqlListNow);

    var sql={};
    sql.sqlcount = sql_count;
    sql.sqllistlimit  = sql_count_list_limit;

    apiServiceHelper(req,sql,callback);

}

var apiServiceHelper = function serviceHelper(req,sql,callback)
{
    var size = Number(req.params.size);
    var index = (Number(req.params.page) - 1) * size;

    var sql_count = sql.sqlcount;
    var sql_count_list_limit = sql.sqllistlimit;

    async.series([
        function (callbackInner) {
            logger.debug(sql_count);
            mysqlUtil.queryBI(sql_count, function (err, result) {
                if (err) {
                    callbackInner(err, null);
                } else {
                    var count = result[0].count;
                    var temp = {count: count, totalPage: Math.ceil(count / size)};
                    callbackInner(err, temp);
                }
            });
        },
        function (callbackInner) {
            logger.debug(sql_count_list_limit);
            mysqlUtil.queryBI(sql_count_list_limit, callbackInner);
        }
    ], callback);
}



